using System;
using System.Web;
using PetShop.BusinessEntities;
using PetShop.BusinessFacade;
using System.Collections.Generic;
using PetShop.Web.Entities;

namespace PetShop.Web.ProcessFlow{

	
	
	
	public class CartController{

	  
		private const string BILLING_KEY = "BILLING_KEY";
		private const string SHIPPING_KEY = "SHIPPING_KEY";
		private const string ACCOUNT_KEY = "ACCOUNT_KEY";
		private const string CART_KEY = "CART_KEY";
		private const string CREDITCARD_KEY = "CREDITCARD_KEY";
		private const string URL_NOCART = "ShoppingCart.aspx";

		public CartController(){}

		
		
		
		
		
		public CartInfo GetCartInfo(bool create){
			CartInfo cart;

			cart = (CartInfo)HttpContext.Current.Session[CART_KEY];

			if ( cart == null ){
				if (create){
					cart = new CartInfo();
				}else{			
					HttpContext.Current.Server.Transfer(URL_NOCART);
					return null;
				}
			}
			
			return cart;
		}


		private  CartItemInfo ConverToCartItemInfo(ItemInfo data)
		{
			const int qty = 1;
			bool inStock = data.Quantity >= 1;
			decimal price = (decimal)data.Price;
			string name = data.Name;
			string dataId = data.Id;
			CartItemInfo newItem = new CartItemInfo(dataId, name, inStock, qty, price);
			return newItem;
		}

		public void AddItemToCart(CartInfo cart, string ItemId)
		{
			var cFcd = new CatalogFacade();
			ItemInfo item = cFcd.GetItemInfoById(ItemId);

			CartItemInfo data = ConverToCartItemInfo(item);

			var fcd = new OrderFacade();


			foreach (CartItemInfo cartItem in cart.Items)
			{
				if (item.Id == cartItem.ItemId)
				{
					cartItem.Quantity++;
					bool enoughInStock = fcd.VerifyAvailability(cartItem.ItemId, cartItem.Quantity);
					cartItem.InStock = enoughInStock;
					decimal totalItem = cartItem.Price * cartItem.Quantity;
					cart.Total = cart.Total + totalItem;
					return;
				}
			}


			cart.Add(data);

		 

		  
		  
		}
		
		
		public void StoreCart(CartInfo cart){
		
			
			HttpContext.Current.Session[CART_KEY] = cart;

		}





		public OrderInfo PurchaseCart()
		{

			CartInfo cart;
			cart = (CartInfo)HttpContext.Current.Session[CART_KEY];


			if ((cart == null) || (cart.Count == 0))
			{

				HttpContext.Current.Server.Transfer(URL_NOCART);

				return null;

			}
			else
			{


				OrderInfo newOrder = new OrderInfo();
				newOrder.UserId = ((AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY]).UserId;
				newOrder.CreditCard = (CreditCardInfo)HttpContext.Current.Session[CREDITCARD_KEY];
				newOrder.BillingAddress = (AddressInfo)HttpContext.Current.Session[BILLING_KEY];
				newOrder.ShippingAddress = (AddressInfo)HttpContext.Current.Session[SHIPPING_KEY];

				List<LineItemInfo> OrderLineItems = ConverToOrderLineItems(cart);
				newOrder.LineItems = OrderLineItems.ToArray();

				newOrder.OrderTotal = cart.Total;
				newOrder.Date = DateTime.Now;


				OrderFacade order = new OrderFacade();
				newOrder.OrderId = order.CreateOrder(newOrder);


				HttpContext.Current.Session[CART_KEY] = null;
				HttpContext.Current.Session[CREDITCARD_KEY] = null;
				HttpContext.Current.Session[BILLING_KEY] = null;
				HttpContext.Current.Session[SHIPPING_KEY] = null;

				return newOrder;
			}
		}


		private List<LineItemInfo> ConverToOrderLineItems(CartInfo cart)
		{

			List<LineItemInfo> orderLineItems = new List<LineItemInfo>();

			int lineNum = 1;

			foreach (CartItemInfo item in cart.Items)
			{

				LineItemInfo lineItem = CreateLineItem(lineNum, item);
				orderLineItems.Add(lineItem);
				lineNum++;
			}

			return orderLineItems;
		}

		private static LineItemInfo CreateLineItem(int lineNum, CartItemInfo item)
		{
			LineItemInfo lineItem = new LineItemInfo(item.ItemId, item.Name, lineNum, item.Quantity, item.Price);
			return lineItem;
		}
		
		
		public void StoreCreditCard(CreditCardInfo creditCard){

			HttpContext.Current.Session[CREDITCARD_KEY] = creditCard;
				
		}

		
		
		
		public void StoreBillingAddress(AddressInfo billingAddress){

			HttpContext.Current.Session[BILLING_KEY] = billingAddress;
				
		}

		
		
		
		public void StoreShippingAddress(AddressInfo shippingAddress){

			HttpContext.Current.Session[SHIPPING_KEY] = shippingAddress;
				
		}

		
		
		
		public AddressInfo GetBillingAddress(){

			return (AddressInfo)HttpContext.Current.Session[BILLING_KEY];
				
		}

		
		
		
		public AddressInfo GetShippingAddress(){

			return (AddressInfo)HttpContext.Current.Session[SHIPPING_KEY];
				
		}

		
		
		
		
		public void ContinueOrder(bool useBillingAddress){

			
			if (useBillingAddress) {
				HttpContext.Current.Session[SHIPPING_KEY] = HttpContext.Current.Session[BILLING_KEY];
			} else {
				
				HttpContext.Current.Response.Redirect("OrderShipping.aspx", true);
			}				
		}

		
		
		
		public void SetAlternateShippingAddress(AddressInfo shippingAddress){

			HttpContext.Current.Session[SHIPPING_KEY] = shippingAddress;
		}
	}
}
